home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / OUTPUT / LOADED_I.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  7.9 KB  |  266 lines

  1. package sub_arctic.output;
  2.  
  3. import sub_arctic.lib.manager;
  4. import sub_arctic.lib.sub_arctic_error;
  5.  
  6. import java.awt.Image;
  7. import java.awt.image.MemoryImageSource;
  8. import java.awt.image.FilteredImageSource;
  9. import java.awt.Component;
  10. import java.awt.Color;
  11. import java.awt.MediaTracker;
  12.  
  13.  
  14. /** 
  15.  * A class to encapsulate a potentially asynchronously loaded image.  This
  16.  * class does blocking for operations that need to wait until the image 
  17.  * is fully loaded (such as requests for the image's size). 
  18.  *
  19.  * @author Scott Hudson
  20.  */
  21. public class loaded_image {
  22.  
  23.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  24.  
  25.   /** 
  26.    * Construct from an Image (this image is marked as not loaded yet).
  27.    * @param Image img the Image to create the loaded_image from
  28.    */
  29.   public loaded_image(Image img)
  30.     {
  31.       _image = img;
  32.       is_loaded = false;
  33.     }
  34.  
  35.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  36.  
  37.   /** 
  38.    * Construct a blank in memory image of the given size and force it
  39.    * to be "loaded" (not clear what that means for in-memory images, but
  40.    * it seems to be required). The image is filled with the systems default
  41.    * background color.
  42.    *
  43.    * @param int w the width of the image in pixels.
  44.    * @param int h the height of the image in pixels.
  45.    */
  46.   public loaded_image(int w, int h)
  47.     {
  48.       drawable d;
  49.       Color c;
  50.       color_scheme cs=style_manager.default_color_scheme();
  51.  
  52.       /* create the empty image */
  53.       _image = manager.an_awt_component().createImage(w,h);
  54.  
  55.       /* force a wait */
  56.         /* ... except on Netscape 3.0 on the mac which hangs ... */
  57.       if (manager.need_workaround(manager.WA_NS3_MAC, manager.WA_TRACKER_HANGS))
  58.     is_loaded = true;
  59.       else
  60.         image();
  61.  
  62.       /* clear the base color so netscape 2.0x doesn't screw us */
  63.       d=get_drawable();
  64.       c=d.getColor();
  65.       d.setColor(cs.base());
  66.       d.fillRect(0,0,width(),height());
  67.       d.setColor(c);
  68.     }
  69.  
  70.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  71.  
  72.   /** 
  73.    *Construct from in memory data. This image is marked as unloaded.
  74.    *
  75.    * @param int[] the data to build the image from
  76.    * @param int w the width of the image
  77.    * @param int h the height of the image 
  78.    */
  79.   public loaded_image(int[] data, int w, int h)
  80.     {
  81.       _image = manager.default_toolkit().createImage(
  82.                     new MemoryImageSource(w,h,data,0,w));
  83.       is_loaded = false;
  84.  
  85.       /* ... unless of course we are on Netscape 3.0 on the mac ...*/
  86.       if (manager.need_workaround(manager.WA_NS3_MAC, manager.WA_TRACKER_HANGS))
  87.         is_loaded = true;
  88.     }
  89.  
  90.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  91.  
  92.   /** Do we know that the image has been loaded */
  93.   protected boolean is_loaded = false;
  94.  
  95.   /** The image we encapsulate. */
  96.   protected Image _image;
  97.   
  98.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  99.  
  100.   /** 
  101.    * Get access to the encapsulated image without blocking if it is not 
  102.    * loaded yet.
  103.    * @return Image the raw image (might not be loaded)
  104.    */
  105.   public Image raw_image() {return _image;}
  106.  
  107.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  108.  
  109.   /** 
  110.    * Get the image, but block if it is not completely loaded. 
  111.    * @return Image the encapsulated Image
  112.    */
  113.   public Image image() 
  114.     {
  115.       boolean ok;
  116.  
  117.       /* if we know for sure that its loaded just return it. */
  118.       if (is_loaded) return _image;
  119.  
  120.       /* use the manager utility routine to wait for it */
  121.       ok = manager.wait_for_image(_image);
  122.  
  123.       /* if there was an error, substitute the "broken image" image */
  124.       if (!ok || _image == null) 
  125.     {
  126.       _image = manager.broken_image_icon().raw_image();
  127.     } 
  128.  
  129.       /* image is now loaded */
  130.       is_loaded = true;
  131.  
  132.       return _image;
  133.     }
  134.  
  135.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  136.  
  137.   /** 
  138.    * Get the image width, waiting for the image to load if necessary 
  139.    * @return int the width of the loaded_image in pixels
  140.    */
  141.   public int width()
  142.     {
  143.       int w;
  144.  
  145.       /* if the image is not currently reporting a width, wait for it */
  146.       w = _image.getWidth(manager.an_observer()); 
  147.       if (w < 0) 
  148.     {
  149.       image();
  150.           w = _image.getWidth(manager.an_observer()); 
  151.     }
  152.  
  153.       /* if its still not reporting a width we have some sort of problem */
  154.       if (w < 0) throw new sub_arctic_error("Improper width in loaded_image");
  155.  
  156.       return w;
  157.     }
  158.  
  159.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  160.  
  161.   /** 
  162.    * Get the image height, waiting for the image to load if necessary 
  163.    * @return int the height of the loaded_image in pixels
  164.    */
  165.   public int height()
  166.     {
  167.       int h;
  168.  
  169.       /* if the image is not currently reporting a height, wait for it */
  170.       h = _image.getHeight(manager.an_observer()); 
  171.       if (h < 0) 
  172.     {
  173.       image();
  174.           h = _image.getHeight(manager.an_observer()); 
  175.     }
  176.  
  177.       /* if its still not reporting a height we have some sort of problem */
  178.       if (h < 0) throw new sub_arctic_error("Improper height in loaded_image");
  179.  
  180.       return h;
  181.     }
  182.  
  183.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  184.  
  185.   /** 
  186.    * Get a drawable that can be used to draw on this image (similar to 
  187.    * getGraphics() on an AWT Image object).  
  188.    * @return drawable a context for drawing on this image
  189.    */
  190.  
  191.   public drawable get_drawable()
  192.     {
  193.       /* build a drawable from the Graphics object we get from the raw image */
  194.       return new drawable(raw_image().getGraphics());
  195.     }
  196.  
  197.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  198.  
  199.   /**
  200.    * This function creates a nice looking loaded_image from an 
  201.    * an intensity image and a color. The intensity image is a black
  202.    * and white image where the ti parameter (RGB (ti,ti,ti) means 
  203.    * transparent. <p>
  204.    * 
  205.    * All shades of grey (other than the transparency intensity)
  206.    * are mapped to an intensity in of the color passed, proportional 
  207.    * to their value (between 0 and 255).<p>
  208.    * 
  209.    * Although these will get you "feathered" images, there is not
  210.    * a lot of dynamic range before your chosen color falls off to
  211.    * black. I would recommend making your intensity map have the
  212.    * values 180+ to get reasonable results.<p>
  213.    *
  214.    * @param loaded_image map   the intensity map.
  215.    * @param Color        color the target color.
  216.    * @param int          ti    this the intensity which should be transparent.
  217.    * @return loaded_image the result image.
  218.    */
  219.   public static loaded_image image_from_intensity_map(loaded_image map,
  220.                               Color color,
  221.                               int ti) {
  222.     
  223.     Image final_image,image;
  224.     Component comp=manager.an_awt_component();
  225.     MediaTracker mt=new MediaTracker(manager.an_awt_component());
  226.  
  227.     /* get the image ready */
  228.     image=map.image();
  229.  
  230.     /* create the filter */
  231.     mask_filter mf=new mask_filter(color,ti);
  232.  
  233.     /* process image with mask filter */
  234.     final_image=
  235.       comp.createImage(new FilteredImageSource(image.getSource(),mf));
  236.     mt.addImage(final_image,0);
  237.     try {
  238.       mt.waitForID(0);
  239.     } catch (InterruptedException e) {
  240.       // very unexpected...
  241.       manager.handle_unexpected_exception(e);
  242.     }
  243.  
  244.     /* make a loaded image and return it */
  245.     return new loaded_image(final_image);
  246.   }
  247.  
  248.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  249. }
  250. /*=========================== COPYRIGHT NOTICE ===========================
  251.  
  252. This file is part of the subArctic user interface toolkit.
  253.  
  254. Copyright (c) 1996 Scott Hudson and Ian Smith
  255. All rights reserved.
  256.  
  257. The subArctic system is freely available for most uses under the terms
  258. and conditions described in 
  259.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  260. and appearing in full in the lib/interactor.java source file.
  261.  
  262. The current release and additional information about this software can be 
  263. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  264.  
  265. ========================================================================*/
  266.